package com.gmrz.uap.model;

import com.jfinal.kit.PathKit;
import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
import com.jfinal.plugin.activerecord.generator.Generator;
import com.jfinal.plugin.activerecord.generator.MetaBuilder;
import com.jfinal.plugin.activerecord.generator.TableMeta;
import com.jfinal.plugin.druid.DruidPlugin;

import javax.sql.DataSource;
import java.util.List;

/**
 * Model、BaseModel、_MappingKit 生成器
 */
public class _Generator {

    /**
     * 部分功能使用 Db + Record 模式实现，无需生成 model 的 table 在此配置
     */
    private static String[] excludedTable = {
            "QRTZ_BLOB_TRIGGERS",
            "QRTZ_CALENDARS",
            "QRTZ_CRON_TRIGGERS",
            "QRTZ_FIRED_TRIGGERS",
            "QRTZ_JOB_DETAILS",
            "QRTZ_LOCKS",
            "QRTZ_PAUSED_TRIGGER_GRPS",
            "QRTZ_SCHEDULER_STATE",
            "QRTZ_SIMPLE_TRIGGERS",
            "QRTZ_SIMPROP_TRIGGERS",
            "QRTZ_TRIGGERS",
            "t_faceprint_data",
            "t_certificate"
    };

    /**
     * 重用 JFinalClubConfig 中的数据源配置，避免冗余配置
     */
    public static DataSource getDataSource() {
        Prop p = PropKit.use("application.properties");
        DruidPlugin druidPlugin = new DruidPlugin(p.get("jdbc.url"), p.get("jdbc.username"), p.get("jdbc.password").trim());
        druidPlugin.start();
        return druidPlugin.getDataSource();
    }

    public static void main(String[] args) throws Exception {
        // base model 所使用的包名
        String baseModelPackageName = "com.gmrz.uap.model.base";
        // base model 文件保存路径
        String baseModelOutputDir = PathKit.getWebRootPath()
                + "/src/main/java/com/gmrz/uap/model/base";

        System.out.println("输出路径：" + baseModelOutputDir);

        // model 所使用的包名 (MappingKit 默认使用的包名)
        String modelPackageName = "com.gmrz.uap.model";
        // model 文件保存路径 (MappingKit 与 DataDictionary 文件默认保存路径)
        String modelOutputDir = baseModelOutputDir + "/..";

        // 创建生成器
        Generator gen = new Generator(getDataSource(), baseModelPackageName, baseModelOutputDir, modelPackageName, modelOutputDir);
        // 设置数据库方言
        gen.setDialect(new MysqlDialect());
        // 添加不需要生成的表名
        for (String table : excludedTable) {
            gen.addExcludedTable(table);
        }

        // 设置是否在 Model 中生成 dao 对象
        gen.setGenerateDaoInModel(true);
        // 设置是否生成字典文件
        gen.setGenerateDataDictionary(true);
        // 设置需要被移除的表名前缀用于生成modelName。例如表名 "osc_user"，移除前缀 "osc_"后生成的model名为 "User"而非 OscUser
        gen.setRemovedTableNamePrefixes("t_");
        // 生成
        gen.generate();


        MetaBuilder metaBuilder = new MetaBuilder(getDataSource());
        List<TableMeta> tableMetas = metaBuilder.build();

        //生成JS通用常量
        String databaseJSPath = PathKit.getWebRootPath() + "/src/main/webapp/static/js/DbConst.js";
        _BuildDatabaseJS buildDatabaseJS = new _BuildDatabaseJS(tableMetas, databaseJSPath);
        buildDatabaseJS.build();
        System.out.println("JS常量文件路径 ==> " + databaseJSPath);

        //生成JAVA通用常量
        String databaseConstPath = PathKit.getWebRootPath() + "/src/main/java/com/gmrz/uap/common/DBCommon.java";
        _BuildDatabaseConst buildDatabaseConst = new _BuildDatabaseConst(tableMetas, databaseConstPath);
        buildDatabaseConst.build();
        System.out.println("JAVA常量文件路径 ==> " + databaseConstPath);
    }
}
